package com.jinlejia.erp.ui.new_house.adapter;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.WeightAddHouseLayoutBinding;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class RecommendHouseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<Map<String, String>> data = new ArrayList<>();

    public RecommendHouseAdapter() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "添加楼盘");
        map.put("canDelete", "false");
        data.add(map);
    }

    public JSONArray getData() throws JSONException {
        JSONArray result = new JSONArray();
        for (Map<String, String> map : data) {
            JSONObject newMap = new JSONObject();
            if (!TextUtils.isEmpty(map.get("code"))) {
                newMap.put("houseCode", map.get("code"));
                newMap.put("houseName", map.get("name"));
                newMap.put("accessTime", map.get("serverTime"));
                result.put(newMap);
            }
        }
        return result;
    }

    public String check() {
        String message = null;
        if (data.size() == 1) {
            Map<String, String> map = data.get(0);
            if ("添加楼盘".equals(map.get("name")) && TextUtils.isEmpty(map.get("code"))) {
                message = "请选择楼盘";
                return message;
            }
        }
        for (Map<String, String> map : data) {
            if (!TextUtils.isEmpty(map.get("code")) && "请选择预计到访时间".equals(map.get("time"))) {
                message = "请选择预计到访时间";
            }
        }
        return message;
    }

    public void addHouseName(String code, String name) {
        Map<String, String> tempMap = null;
        int position = -1;
        for (int i = 0; i < data.size(); i++) {
            Map<String, String> map = data.get(i);
            if (TextUtils.equals(map.get("code"), code)) {
                tempMap = map;
                position = i;
                break;
            }
        }
        if (tempMap != null) { //以前已经有了这个数据,则不再添加
            tempMap.put("canDelete", "true");
            notifyItemChanged(position);
        } else {
            Map<String, String> map = data.get(data.size() - 1);
            map.put("code", code);
            map.put("name", name);
            map.put("canDelete", "true");
            notifyItemChanged(data.size() - 1);
            Map<String, String> temp = new HashMap<>();
            temp.put("name", "添加楼盘");
            temp.put("canDelete", "false");
            data.add(temp);
            notifyItemInserted(data.size());
        }
    }

    public void setHouseName(String code, String name, int position) {
        Map<String, String> map = data.get(position);
        if (TextUtils.isEmpty(code) || TextUtils.isEmpty(name)) return; //当房间名称和CODE任意一个为null时，直接返回
        map.put("name", name);
        map.put("code", code);
        map.put("canDelete", "true");
        notifyItemChanged(position);
        Map<String, String> temp = new HashMap<>();
        temp.put("name", "添加楼盘");
        temp.put("canDelete", "false");
        data.add(temp);
        notifyItemInserted(data.size());
    }

    public void setTime(String time, String serverTime, String positionStr) {
        int position;
        try {
            position = Integer.parseInt(positionStr);
        } catch (Exception ignore) {
            return;
        }
        Map<String, String> map = data.get(position);
        map.put("time", time);
        map.put("serverTime", serverTime);
        notifyItemChanged(position);
    }

    @Override
    public HouseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.weight_add_house_layout, parent, false);
        return new HouseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (!(holder instanceof HouseViewHolder)) {
            return;
        }
        final Map<String, String> map = data.get(position);
        WeightAddHouseLayoutBinding binding = DataBindingUtil.getBinding(holder.itemView);
        if ("添加楼盘".equals(map.get("name")) && TextUtils.isEmpty(map.get("code"))) {
            map.put("time", "");
            binding.rootLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    addHouseEvent.selectHouse(map, holder.getAdapterPosition());
                }
            });
        } else {
            binding.rootLayout.setOnClickListener(null);
            if (TextUtils.isEmpty(map.get("time"))) {
                map.put("time", "请选择预计到访时间");
            }
            binding.timeTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    addHouseEvent.selectTime(map, holder.getAdapterPosition());
                }
            });
        }
        boolean delete = Boolean.parseBoolean(map.get("canDelete"));
        if (delete) {
            binding.deleteHouseTextView.setVisibility(View.VISIBLE);
            binding.arrowImageView.setVisibility(View.GONE);
        } else {
            binding.deleteHouseTextView.setVisibility(View.GONE);
            binding.arrowImageView.setVisibility(View.VISIBLE);
        }
        //删除楼盘
        binding.deleteHouseTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                data.remove(map);
                int position = holder.getAdapterPosition();
                notifyItemRemoved(position);
            }
        });

        binding.setMap(map);

    }

    @Override
    public int getItemCount() {
        if (data == null || data.isEmpty()) {
            return 1;
        }
        return data.size();
    }

    private AddHouseEvent addHouseEvent;

    public void setAddHouseEvent(AddHouseEvent addHouseEvent) {
        this.addHouseEvent = addHouseEvent;
    }

    public interface AddHouseEvent {
        void selectTime(Map<String, String> map, int position);

        void selectHouse(Map<String, String> map, int position);
    }

    private static class HouseViewHolder extends RecyclerView.ViewHolder {

        private HouseViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }
}

